﻿using Admin.NET.Application.Util.Extension;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Radius.NET.Interface.Kingdee.Entity
{
    /// <summary>
    /// 采购入库单
    /// </summary>
    public class STK_InStock : KingdeePubClass
    {
        public STK_InStockModel Model { get; set; }

        #region 采购入库单表头
        public class STK_InStockModel
        {
            /// <summary>
            /// 
            /// </summary>
            public int FID { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public FBillTypeID FBillTypeID { get; set; }

            public string FBillNo { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public string FBusinessType { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public DateTime? FDate { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public FStockOrgId FStockOrgId { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public FDemandOrgId FDemandOrgId { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public FPurchaseOrgId FPurchaseOrgId { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public FSUPPLIERID FSupplierId { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public FSupplyId FSupplyId { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public FSettleId FSettleId { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public FChargeId FChargeId { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public string FOwnerTypeIdHead { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public FOwnerIdHead FOwnerIdHead { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public string FSplitBillType { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public List<FInStockEntryItem> FInStockEntry { get; set; }
        }
        #endregion

        #region 采购入库单明细
        public class FInStockEntryItem
        {
            /// <summary>
            /// 
            /// </summary>
            public int FEntryID { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public string FPOOrderNo { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public string FRowType { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public FMaterialId FMaterialId { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public string FMaterialDesc { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public FUnitID FUnitID { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public double? FRealQty { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public FPriceUnitID FPriceUnitID { get; set; }

            public FAuxPropId FAuxPropId { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public FLot FLot { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public FStockId FStockId { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public FStockLocId FStockLocId { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public FStockStatusId FStockStatusId { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public string FOWNERTYPEID { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public bool FCheckInComing { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public bool FIsReceiveUpdateStock { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public double? FPriceBaseQty { get; set; }

            public double? FEntryTaxAmount { get; set; }
            public double? FAllAmount_LC { get; set; }
            public double? FAllAmount { get; set; }
            public double? FTaxAmount_LC { get; set; }
            public double? FAmount { get; set; }
            public double? FAmount_LC { get; set; }


            /// <summary>
            /// 
            /// </summary>
            public FRemainInStockUnitId FRemainInStockUnitId { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public bool FBILLINGCLOSE { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public double? FRemainInStockQty { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public double? FAPNotJoinQty { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public double? FExtAuxUnitQty { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public double? FRemainInStockBaseQty { get; set; }

            public double? FTaxPrice { get; set; }

            public double? FTaxNetPrice { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public double? FDiscountRate { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public double? FEntryTaxRate { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public FOwnerId FOWNERID { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public string FSRCBILLTYPEID { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public string FSRCBillNo { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public int FSRCRowId { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public string FProduceDate { get; set; }


            /// <summary>
            /// 
            /// </summary>
            public string FExpiryDate { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public DateTime? F_PYBU_SCRQ { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public List<FInStockEntry_LinkItem> FInStockEntry_Link { get; set; }
            public double FPrice { get; internal set; }
            /// <summary>
            /// 入库类型
            /// </summary>
            public string? FWWInType { set; get; }
        }
        #endregion

        #region 采购入库单明细关联表
        /// <summary>
        /// 采购入库单明细关联表
        /// </summary>
        public class FInStockEntry_LinkItem
        {
            /// <summary>
            /// 
            /// </summary>
            public string FInStockEntry_Link_FRuleId { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public string FInStockEntry_Link_FSTableName { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public int FInStockEntry_Link_FSBillId { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public int FInStockEntry_Link_FSId { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public double? FInStockEntry_Link_FBaseUnitQtyOld { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public double? FInStockEntry_Link_FBaseUnitQty { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public double? FInStockEntry_Link_FRemainInStockBaseQtyOld { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public double? FInStockEntry_Link_FRemainInStockBaseQty { get; set; }

        }
        #endregion

        public class FAuxPropId
        {
            public FAUXPROPID__FF100001_Model FAUXPROPID__FF100001 {  get; set; }
            public FAUXPROPID__FF100002_Model FAUXPROPID__FF100002 { get; set; }
            public class FAUXPROPID__FF100001_Model
            {
                public string FNumber { get; set; } = string.Empty;
            }
            public class FAUXPROPID__FF100002_Model
            {
                public string FNumber { get; set; } = string.Empty;
            }
        }
        public class FStockLocId
        {
            public FSTOCKLOCID__FF100008_Model FSTOCKLOCID__FF100008 { get; set; }
            public class FSTOCKLOCID__FF100008_Model
            {
                public string FNumber { get; set; } = string.Empty;
            }
        }
        
        public object BuildData(InventoryEvidence item, List<PUR_ReceiveBillDto> pUR_ReceiveBillDto)
        {
            var obj = new STK_InStock()
            {
                Model = new ()
                {
                    FID = 0,
                    FBillTypeID = new ()
                    {            //单据类型
                        FNumber = "RKD01_SYS"
                    },
                    FBillNo = item.Evidence_Number,
                    FBusinessType = "CG",		//业务类型
                    FDate = item.Date,			//入库日期
                    FStockOrgId = new()
                    {            //收料组织
                        FNumber = KingdeeConfigConst.ORGID
                    },
                    FDemandOrgId = new()
                    {           //需求组织
                        FNumber = KingdeeConfigConst.ORGID
                    },
                    FPurchaseOrgId = new()
                    {         //采购组织
                        FNumber = KingdeeConfigConst.ORGID
                    },
                    FSupplierId = new()
                    {
                        FNumber = item.ContactNum	//供应商
                    },
                    FSupplyId = new()
                    {
                        FNumber = item.ContactNum	//供货方
                    },
                    FSettleId = new()
                    {
                        FNumber = item.ContactNum	//结算方
                    },
                    FChargeId = new()
                    {
                        FNumber = item.ContactNum	//收款方
                    },
                    FOwnerTypeIdHead = "BD_OwnerOrg",	//货主类型
                    FOwnerIdHead = new()
                    {               //货主
                        FNumber = KingdeeConfigConst.ORGID
                    },
                    FSplitBillType = "A",
                    FInStockEntry = new List<FInStockEntryItem>()
                }
            };

            foreach (var e in item.FG_InvDetail)
            {
                //收料通知单
                var srcItem = pUR_ReceiveBillDto.Find(x => x.FMaterialId_FNumber == e.Inventory_ID && x.FDetailEntity_FSEQ == e.CF01.ParseToInt());
                if (srcItem is not null)
                {

                    obj.Model.FInStockEntry.Add(new FInStockEntryItem()
                    {
                        FEntryID = 0,
                        FPOOrderNo = item.OrderID,
                        FRowType = "Standard",          //产品类型
                        FWWInType = "QLI",              //入库类型
                        FMaterialId = new()
                        {                //物料编码
                            FNumber = e.Inventory_ID
                        },
                        FMaterialDesc = "",         //物料说明
                        FUnitID = new()
                        {                    //库存单位
                            FNumber = e.FG_Inventory.Unit ?? "kg"//e.UMCode
                        },
                        FRealQty = e.Qty,               //实收数量
                        FPriceUnitID = new()
                        {
                            FNumber = e.FG_Inventory.Unit ?? "kg"  //计价单位
                        },
                        FAuxPropId = new()
                        {
                            FAUXPROPID__FF100001 = new()
                            {
                                FNumber = e.LogoID
                            },
                            FAUXPROPID__FF100002 = new()
                            {
                                FNumber = srcItem.FAuxPropId_FF100002_FNumber
                            }
                        },
                        FLot = new()
                        {
                            FNumber = e.ProjectBatch   //批号
                        },
                        FStockId = new()
                        {
                            FNumber = item.DesStock_ID //仓库
                        },
                        FStockLocId = new()
                        {
                            FSTOCKLOCID__FF100008 = new()
                            {
                                FNumber = e.Stk_ID //仓位
                            }
                        },
                        FStockStatusId = new()
                        {
                            FNumber = "KCZT01_SYS" //库存状态
                        },
                        //FGiveAway = e.FGiveAway,		//是否赠品
                        FOWNERTYPEID = "BD_OwnerOrg", //货主类型
                        FCheckInComing = true,
                        FIsReceiveUpdateStock = false,
                        FPriceBaseQty = e.Qty,          //计价基本数量
                        FRemainInStockUnitId = new FRemainInStockUnitId
                        {
                            FNumber = e.FG_Inventory.Unit ?? "kg" //采购单位
                        },
                        FPrice = srcItem.FPrice,   //单价
                        FTaxPrice = srcItem.FTaxPrice,     //含税单价
                        FTaxNetPrice = srcItem.FTaxPrice, //净价
                        FEntryTaxAmount = srcItem.FEntryTaxAmount, //税额
                        FTaxAmount_LC = srcItem.FTaxAmount_LC,   //税额本位币
                        FAllAmount = srcItem.FAllAmount,   //价税合计
                        FAllAmount_LC = srcItem.FAllAmount_LC,   //价税合计本位币  
                        FAmount = srcItem.FAmount,   //金额
                        FAmount_LC = srcItem.FAmount_LC,   //金额本位币
                        FBILLINGCLOSE = false,
                        FRemainInStockQty = e.Qty,      //采购数量
                        FAPNotJoinQty = e.Qty,          //未关联应付数量
                        FExtAuxUnitQty = e.NPKG,
                        FRemainInStockBaseQty = e.Qty,  //采购基本数量
                        //FDiscountRate = e.Discount, //折扣率
                        FEntryTaxRate = srcItem.FEntryTaxRate,      //税率%
                        FOWNERID = new()
                        {                   //货主
                            FNumber = KingdeeConfigConst.ORGID
                        },
                        FSRCBILLTYPEID = "PUR_ReceiveBill", //源单类型
                        FSRCBillNo = item.IQCNo,                //源单编号
                        FSRCRowId = srcItem.FDetailEntity_FEntryID,             //源单行内码
                        FProduceDate = e.MakeBeginDate?.ToString("yyyy-MM-dd"),      //生产日期（新）
                        FExpiryDate = e.EndDate?.ToString("yyyy-MM-dd"),  //有效期至
                        FInStockEntry_Link = new List<FInStockEntry_LinkItem>() {
                           new () {
                                FInStockEntry_Link_FRuleId = "PUR_ReceiveBill-STK_InStock",	//转换规则
                                FInStockEntry_Link_FSTableName = "T_PUR_ReceiveEntry",			//源单表
                                FInStockEntry_Link_FSBillId = srcItem.FID,						//源单内码
                                FInStockEntry_Link_FSId = srcItem.FDetailEntity_FEntryID,							//源单分录内码
                                FInStockEntry_Link_FBaseUnitQtyOld = e.Qty,					//原始携带量
                                FInStockEntry_Link_FBaseUnitQty = e.Qty,						//原始携带量
                                FInStockEntry_Link_FRemainInStockBaseQtyOld = e.Qty,			//原始携带量
                                FInStockEntry_Link_FRemainInStockBaseQty = e.Qty				//原始携带量
                           }
                        }
                    });
                }
            }

            return obj;
        }
    }
}
